#ifndef __BMS_PROTECT_H__
#define __BMS_PROTECT_H__

#include "bms_config.h"
#include "bms_hal_config.h"

typedef struct
{
	float ShoutdownVoltage;            // unit : V
	float OVProtect;		               // unit : V; Implemented by the BQ chip.
	float OVRelieve;                   // unit : V; Implemented by the BQ chip.
	float UVProtect;                   // unit : V
	float UVRelieve;                   // unit : V
	                                   
	float OCCProtect;                  // Overcurrent charging threshold,  unit : A
	float OCDProtect;                  // Overcurrent discharge threshold, unit : A; Implemented by the BQ chip.
	                                   
	BMS_OVDelayTypedef OVDelay;        // Overvoltage protection delay during charging; Implemented by the BQ chip.
	BMS_UVDelayTypedef UVDelay;	       // Discharge undervoltage protection delay; Implemented by the BQ chip.
	BMS_OCDDelayTypedef OCDDelay;      // Discharge overcurrent protection delay; Implemented by the BQ chip.
	BMS_SCDDelayTypedef SCDDelay;      // Discharge short-circuit protection delay; Implemented by the BQ chip.
	                                   
	uint8_t OCDRelieve;                // Discharge overcurrent recovery, unit : seconds
	uint8_t SCDRelieve;	               // Discharge short-circuit recovery, unit : seconds
	uint8_t OCCDelay;                  // Charging overcurrent delay, unit : seconds
	uint8_t OCCRelieve;                // Charging overcurrent recovery, unit : seconds
	                                   
	float OTCProtect;		               // Over-temperature protection during charging, unit: ℃
	float OTCRelieve;                  // Over-temperature release during charging, unit: ℃
	float OTDProtect;							     // Over-temperature protection during discharge, unit: ℃
	float OTDRelieve;                  // Over-temperature release during discharge, unit: ℃
	                                   
	float LTCProtect;                  // Low-temperature protection during charging, unit: ℃
	float LTCRelieve;                  // Low-temperature release during charging, unit: ℃
	float LTDProtect;                  // Low-temperature release during discharge, unit: ℃
	float LTDRelieve;                  // Discharge low-temperature release, unit: ℃	
}BMS_ProtectParamTypedef;


// Enumeration of alarm events
typedef enum
{
	FLAG_ALERT_NO	      = 0x0000,		   // No alarm triggered
	                                   
	FLAG_ALERT_OV	      = 0X0001,		   // Overvoltage protection triggered during charging;     Implemented by the BQ chip.
	FLAG_ALERT_OCC	    = 0X0002,		   // Overcurrent protection triggered during charging;     Software triggered
	FLAG_ALERT_OTC	    = 0X0004,		   // Overtemperature protection triggered during charging; Software triggered
	FLAG_ALERT_LTC	    = 0X0008,		   // Low-temperature protection triggered during charging; Software triggered
                                     
	FLAG_ALERT_CHG_MASK = 0x000F,	     // Charging alarm mask
	                                   
	FLAG_ALERT_UV	      = 0X0010,		   // Undervoltage protection triggered during discharge;   Implemented by the BQ chip.
	FLAG_ALERT_OCD	    = 0X0020,		   // Overcurrent protection triggered during discharge;    Implemented by the BQ chip.
	FLAG_ALERT_SCD	    = 0X0040,		   // Short-circuit protection triggered during discharge;  Implemented by the BQ chip.
	FLAG_ALERT_OTD	    = 0X0080,		   // Overtemperature protection triggered during discharge;Software triggered
	FLAG_ALERT_LTD	    = 0X0100,		   // Low-temperature protection triggered during discharge;Software triggered
                                     
	FLAG_ALERT_DSG_MASK = 0x01F0,	     // Discharge alarm mask
} BMS_ProtectAlertTypedef;

typedef struct
{
	BMS_ProtectParamTypedef param;
	BMS_ProtectAlertTypedef alert;
}BMS_ProtectTypedef;

extern BMS_ProtectTypedef BMS_Protect;

extern void BMS_ProtectInit(void);
extern void BMS_ProtectHwOCD(void);
extern void BMS_ProtectHwSCD(void);
extern void BMS_ProtectHwOV(void);
extern void BMS_ProtectHwUV(void);

#endif /* __BMS_PROTECT_H__ */
